﻿using System;
using System.Collections.Generic;
using Wicresoft.SelfService.DAL;
using Wicresoft.SelfService.DAO;
using System.Linq;

namespace Wicresoft.SelfService.BL
{
    public class workListHelper
    {
        private static WorkListDAL wld;
        private static ITServiceDAL itService;
        private static ITSMAccidentDAL itsm;
        public workListHelper()
        {
            if (wld == null) wld = new WorkListDAL();
            if (itService == null) itService = new ITServiceDAL();
            if (itsm == null) itsm = new ITSMAccidentDAL();
        }
        public WorkListCounter getWorkListCounter(string userID)
        {
            WorkListCounter workListCounter = new WorkListCounter();
            string company = itService.getUserInfo_By_AccountID(userID).company;
            workListCounter = wld.getWorkListCounter(userID, company);
            return workListCounter;
        }
        /// <summary>
        ///我的待办
        /// </summary>
        public List<ServiceDetail> getTodoList(string userID, int? serviceTypeID, string requestID, string userName, Nullable<int> ps, Nullable<int> pi, DateTime? startTime, DateTime? endTime, string description)
        {
            string company = itService.getUserInfo_By_AccountID(userID).company;
            OrderbyParameters[] param = new OrderbyParameters[] { new OrderbyParameters() { orderByCol = "createTime", orderByType = orderByType.descending } };
            List<ServiceDetail> list = wld.getTodoList_By_Query(userID, company, serviceTypeID, requestID, userName, param, ps, pi, startTime, endTime, description);
            return list;
        }
        public int getTodoListCount(string userID, int? serviceTypeID, string requestID, string userName, DateTime? startTime, DateTime? endTime, string description)
        {
            string company = itService.getUserInfo_By_AccountID(userID).company;
            int count = wld.getTodoListCount_By_Query(userID, company, serviceTypeID, requestID, userName, startTime, endTime, description);
            return count;
        }
        /// <summary>
        /// 我的已办
        /// </summary>
        public List<ServiceDetail> getDoneList(string userID, string requestID, string description, Nullable<int> serviceTypeID, Nullable<int> status, string userName, DateTime? startTime, DateTime? endTime, Nullable<int> ps, Nullable<int> pi)
        {
            string company = itService.getUserInfo_By_AccountID(userID).company;
            OrderbyParameters[] param = new OrderbyParameters[] { new OrderbyParameters() { orderByCol = "createTime", orderByType = orderByType.descending } };
            List<ServiceDetail> list = wld.getDoneList_By_Query(userID, requestID, description, serviceTypeID, status, userName, startTime, endTime, param, ps, pi);

            return list;
        }
        public int getDoneListCount(string userID, string requestID, string description, Nullable<int> serviceTypeID, Nullable<int> status, string userName, Nullable<DateTime> startTime, Nullable<DateTime> endTime)
        {
            int count = wld.getDoneListCount_By_Query(userID, requestID, description, serviceTypeID, status, userName, startTime, endTime);
            return count;
        }
        /// <summary>
        /// 我的申请
        /// </summary>
        public List<ServiceDetail> getMyApplyList(string userID, int submitType, string requestID, string description, Nullable<int> status, Nullable<DateTime> startTime, Nullable<DateTime> endTime, Nullable<int> ps, Nullable<int> pi)
        {
            List<ServiceDetail> list = new List<ServiceDetail>();
            OrderbyParameters[] param = new OrderbyParameters[] { new OrderbyParameters() { orderByCol = "createTime", orderByType = orderByType.descending } };
            //自服务提交
            if (submitType == 1)
            {
                list = wld.getMyApplyList_By_Query(userID, requestID, description, status, startTime, endTime, param, ps, pi);
            }
            //ITSM提交
            else if (submitType == 2)
            {
                list = itsm.getAccident(userID, requestID, description, status, startTime, endTime);
                list.Sort(new mycompare());
                if (ps.HasValue != false && pi.HasValue != false)
                {
                    return list.Skip((pi.Value - 1) * ps.Value).Take(ps.Value).ToList();
                }
            }
            return list;
        }
        public int getMyApplyListCount(string userID, int submitType, string requestID, string description, Nullable<int> status, Nullable<DateTime> startTime, Nullable<DateTime> endTime)
        {
            int count = 0;
            //自服务提交
            if (submitType == 1)
            {
                count = wld.getMyApplyListCount_By_Query(userID, requestID, description, status, startTime, endTime);
            }
            //ITSM提交
            else if (submitType == 2)
            {
                count = itsm.getAccident(userID, requestID, description, status, startTime, endTime).Count;
            }
            return count;
        }

        /// <summary>
        /// 我的草稿
        /// </summary>
        public List<ServiceDetail> getDraftList(string userID, string requestID, string description, Nullable<DateTime> startTime, Nullable<DateTime> endTime, Nullable<int> ps, Nullable<int> pi)
        {
            OrderbyParameters[] param = new OrderbyParameters[] { new OrderbyParameters() { orderByCol = "createTime", orderByType = orderByType.descending } };
            List<ServiceDetail> list = wld.getDraftList_By_Query(userID, requestID, description, startTime, endTime, param, ps, pi);
            return list;
        }
        public int getDraftListCount(string userID, string requestID, string description, Nullable<DateTime> startTime, Nullable<DateTime> endTime)
        {
            int count = wld.getDraftListCount_By_Query(userID, requestID, description, startTime, endTime);
            return count;
        }
        //public void createAccident()
        //{

        //    ITSMAccidentDAL it = new ITSMAccidentDAL();
        //    it.getAccident("Mary", "I", null, 1, null, DateTime.Now);
        //    ServiceDetail serde = new ServiceDetail();
        //    serde.accountID = "sda";
        //    serde.companyIDs = "三明市烟草专卖局";
        //    serde.serviceRequestID = "SR20130824031255119";
        //    serde.appName = "Test";
        //    ServiceList serviceList = new DAO.ServiceList();
        //    serde.serviceTypeID = 2;
        //    serviceList.serviceTypeID = 2;
        //    serviceList.serviceName = "标准安装计算机系统";
        //    serviceList.applvl1 = "桌面终端";
        //    serviceList.applvl2 = "安装调试";
        //    serviceList.applvl3 = "标准安装及接入";
        //    serde.serviceType = serviceList;
        //    serde.description = "my my my pg";
        //    serde.os = "os2";
        //    serde.browser = "browser1";
        //    serde.computerName = "computername1";
        //    serde.ipAdress = "172.33.33.33";
        //    serde.mac = "";
        //    it.createAccident(serde);
        //}

    }

    public class mycompare : IComparer<ServiceDetail> { public int Compare(ServiceDetail x, ServiceDetail y) { return y.createTime.CompareTo(x.createTime); throw new NotImplementedException(); } }
}
